///\todo upgrade

//////////////////////////////////////////////////////////////////////////////////
//                                                                              //
//  This file is part of the buola project (https://code.google.com/p/buola/).  //
//                                                                              //
//  Copyright(c) 2007-2012 Xavi Gratal                                          //
//  gratal AT gmail DOT com                                                     //
//                                                                              //
//  Buola is free software: you can redistribute it and/or modify               //
//  it under the terms of the GNU General Public License as published by        //
//  the Free Software Foundation, either version 3 of the License, or           //
//  (at your option) any later version.                                         //
//                                                                              //
//  Buola is distributed in the hope that it will be useful,                    //
//  but WITHOUT ANY WARRANTY; without even the implied warranty of              //
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the               //
//  GNU General Public License for more details.                                //
//                                                                              //
//  You should have received a copy of the GNU General Public License           //
//  along with buola.  If not, see <http://www.gnu.org/licenses/>.              //
//                                                                              //
//////////////////////////////////////////////////////////////////////////////////

#ifndef _BUOLA_ZZ_ZSYMBOLS_H_
#define _BUOLA_ZZ_ZSYMBOLS_H_

#include <buola/zz/zbase.h>
#include <buola/container/ctrie.h>
#include <memory>

namespace buola { namespace zz {

template<typename tChar=char,typename tAttrType=UUnused,template<typename tC,typename tT> class tTTable=CTrie>
class ZSymbols : public ZBase<ZSymbols<tChar,tAttrType,tTTable>>
{
public:
    template<typename tIt>
    struct tAttribute
    {
        typedef tAttrType tType;
    };

    typedef tTTable<tChar,tAttrType> tTable;
    typedef std::basic_string<tChar> tStr;

    ZSymbols()
        :   mTable(new tTable)
    {}

    ZSymbols(const ZSymbols &pRH)
        :   mTable(pRH.mTable)
    {
    }

    ZSymbols& operator=(const ZSymbols &pRH)
    {
        *mTable=*pRH.mTable;
    }

    void Clear()
    {
        mTable->Clear();
    }

    ZSymbols &operator+=(const tStr &pStr)
    {
        return Add(pStr);
    }

    ZSymbols &operator-=(const tStr &pStr)
    {
        return Remove(pStr);
    }

    template<typename tIt,typename tAttr,typename tCtx,typename tSk>
    bool Parse(tIt &pB,const tIt &pE,tAttr &pAttr,tCtx&,const tSk &pSk) const
    {
        zz::skip_over(pB,pE,pSk);

        tIt lI=pB;
        
        if(const tAttrType *lValue=mTable->GetLongest(lI,pE))
        {
            pB=lI;
            pAttr=*lValue;
            return true;
        }

        return false;
    }

    ZSymbols &Add(const tStr &pStr,const tAttrType &pVal=tAttrType())
    {
        mTable->Add(pStr,pVal);
        return *this;
    }

    ZSymbols &Remove(const tStr &pStr)
    {
        mTable->Remove(pStr);
        return *this;
    }
    
    tTable &GetTable()      {   return *mTable;     }
    
private:
    std::shared_ptr<tTable> mTable;
};

/*namespace zz*/ } /*namespace buola*/ }

#endif
